package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysCmp;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysIndustryMapper;
import com.ruoyi.system.domain.SysIndustry;
import com.ruoyi.system.service.ISysIndustryService;

import javax.annotation.Resource;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-11-20
 */
@Service
public class SysIndustryServiceImpl implements ISysIndustryService
{
    @Resource
    private SysIndustryMapper sysIndustryMapper;

    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public SysIndustry selectSysIndustryById(String id)
    {
        return sysIndustryMapper.selectSysIndustryById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param sysIndustry 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<SysIndustry> selectSysIndustryList(SysIndustry sysIndustry)
    {
        return sysIndustryMapper.selectSysIndustryList(sysIndustry);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param sysIndustry 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertSysIndustry(SysIndustry sysIndustry)
    {
        sysIndustry.setCreateTime(DateUtils.getNowDate());
        return sysIndustryMapper.insertSysIndustry(sysIndustry);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param sysIndustry 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateSysIndustry(SysIndustry sysIndustry)
    {
        sysIndustry.setUpdateTime(DateUtils.getNowDate());
        return sysIndustryMapper.updateSysIndustry(sysIndustry);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSysIndustryByIds(String[] ids)
    {
        return sysIndustryMapper.deleteSysIndustryByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSysIndustryById(String id)
    {
        return sysIndustryMapper.deleteSysIndustryById(id);
    }

    @Override
    public String importIndustry(List<SysIndustry> industryList, Boolean isUpdateSupport, Long userId) {
        if (StringUtils.isNull(industryList) || industryList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        for (SysIndustry sysIndustry : industryList) {
            //验证该条数据是否存在
            SysIndustry sysIndustrySel = new SysIndustry();
            sysIndustrySel.setIndCode(sysIndustry.getIndCode());
            List<SysIndustry> sysIndustryList = sysIndustryMapper.selectSysIndustryList(sysIndustrySel);
            if (sysIndustryList != null && sysIndustryList.size() > 0) {
                if (isUpdateSupport) {
                    sysIndustry.setUpdateUserId(userId);
                    sysIndustry.setUpdateTime(new Date());
                    SysIndustry industry = sysIndustryList.get(0);
                    BeanUtils.copyProperties(sysIndustry, industry);
                    sysIndustryMapper.updateSysIndustry(industry);
                }
            } else {
                //新增
                sysIndustry.setCreateUserId(userId);
                sysIndustry.setCreateTime(new Date());
                sysIndustryMapper.insertSysIndustry(sysIndustry);
            }
        }
        return "success";
    }
}
